using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;

namespace KFGame
{
    public class KFSettingItem
    {

        /// <summary>
		/// 组名
		/// </summary>
        public string group = KFSetting.group;
        /// <summary>
		/// 项的名字，通过名字使用
		/// </summary>
        public string name;
        /// <summary>
		/// 显示标题
		/// </summary>
        public string title;
        /// <summary>
		/// 描述
		/// </summary>
        public string describe;
        /// <summary>
		/// 存的值
		/// </summary>
        public string value;
        /// <summary>
        /// 默认值
        /// </summary>
        public string value_default;
        /// <summary>
		///  存的数据类型
		/// </summary>
        public string value_type = "string";
        /// <summary>
		/// 每增加减少时的步长
		/// </summary>
        public double value_step = 1;
        /// <summary>
		/// 最小值
		/// </summary>
        public double value_mix = 0;
        /// <summary>
		/// 最大值
		/// </summary>
        public double value_max = 0;
        /// <summary>
		/// 类行为选择时
		/// </summary>
        public string value_selectlist;

    }



    public class KFSetting
    {
        /// <summary>
        /// 储存数据类型
        /// </summary>
        public enum DataType
        {
            Type_String = 0,
            Type_Bool = 1,
            Type_Int = 2,
        }

        public static string path = "KFConfig/kfsetting";
        public static string iniPath = Application.streamingAssetsPath + "/" + path + ".ini";
        public const string group = "KFINFO";


        private static KFSetting setting;

        private Dictionary<string, KFSettingItem> items = new Dictionary<string, KFSettingItem>();


       // BackstageStatistic backstage = new BackstageStatistic();


        public static KFSetting instantiation()
        {
            if (setting == null)
            {
                setting = new KFSetting();
                setting.load();
                return setting;
            }
            return setting;
        }


        /// <summary>
        /// 读取所有项
        /// </summary>
        /// <returns></returns>
        public List<string> getItems()
        {
            List<string> names = new List<string>();
            foreach (var item in items.Keys)
                names.Add(item);
            return names;

        }



        /// <summary>
        /// 读取项
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public KFSettingItem getItem(string name)
        {
            KFSettingItem item;
            items.TryGetValue(name, out item);
            return item;
        }


        /// <summary>
        /// 设置数据
        /// </summary>
        /// <param name="item"></param>
        public void SetItem(KFSettingItem item)
        {
            if (items.ContainsKey(item.name))
            {
                items[item.name] = item;
                return;
            }

            items.Add(item.name, item);

        }

        /// <summary>
        /// 设置值
        /// </summary>
        /// <param name="name"></param>
        /// <param name="value"></param>
        public void SetItem(string name, string value)
        {
            if (items.ContainsKey(name))
            {
                KFSettingItem item = items[name];
                item.value = value;
                return;
            }
            KFSettingItem iv = new KFSettingItem();
            iv.name = name;
            iv.value = value;
        }

        /// <summary>
        /// 读取值
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public string ItemValue(string name)
        {
            KFSettingItem item;
            items.TryGetValue(name, out item);
            return item.value;
        }

        /// <summary>
        /// 读取值
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public bool ItemBool(string name)
        {
            return ("1" == ItemValue(name));
        }

        /// <summary>
        /// 读取值
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public int ItemIntegre(string name)
        {
            int a = 0;
            int.TryParse(ItemValue(name), out a);
            return a;
        }

        /// <summary>
        /// 读取值
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public double ItemDouble(string name)
        {
            double a = 0;
            a = Convert.ToDouble(ItemValue(name));
            return a;
        }

        /// <summary>
        /// 读取值
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public float ItemFloat(string name)
        {
            float a = 0f;
            float.TryParse(ItemValue(name), out a);
            return a;
        }


        /// <summary>
        /// 加载数据
        /// </summary>
        void load()
        {
            INIParser iNIParser = new INIParser();
            iNIParser.Open(iniPath);
            List<string> gnames = iNIParser.SectionNames();
            foreach (string group in gnames)
            {
                KFSettingItem item = new KFSettingItem();
                item.group = group;
                item.name = iNIParser.ReadValue(group, "name", "");
                item.title = iNIParser.ReadValue(group, "title", "");
                item.describe = iNIParser.ReadValue(group, "describe", "");
                item.value_type = iNIParser.ReadValue(group, "value_type", "");
                item.value = iNIParser.ReadValue(group, "value", "");
                item.value_default = iNIParser.ReadValue(group, "value_default", "");
                item.value_step = iNIParser.ReadValue(group, "value_step", 1.0);
                item.value_max = iNIParser.ReadValue(group, "value_max", 1.0);
                item.value_mix = iNIParser.ReadValue(group, "value_mix", 0.0);
                item.value_selectlist = iNIParser.ReadValue(group, "value_selectlist", "");
                items.Add(item.name, item);
            }

            iNIParser.Close();

        }

        /// <summary>
		///  保存更新数据
		/// </summary>
        public void save()
        {

            INIParser iNIParser = new INIParser();
            iNIParser.Open(iniPath);
            foreach (var item in items.Values)
            {
                iNIParser.WriteValue(item.group, "value", item.value.ToString());
            }

            iNIParser.Close();

        
        }

    }
}
